package com.wss.merchant.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wss.merchant.domain.*;
import com.wss.merchant.service.DlNewCommunityService;
import com.wss.merchant.service.DlNewStoreService;
import com.wss.merchant.service.DlUserService;
import com.wss.merchant.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 店铺表 前端控制器
 * </p>
 *
 * @author wss
 * @since 2021-03-02
 */
@Slf4j
@Controller
@RequestMapping("/store")
public class DlNewStoreController {

    @Autowired
    private DlNewStoreService dlNewStoreService;
    @Autowired
    private DlUserService dlUserService;
    @Autowired
    private DlNewCommunityService dlNewCommunityService;
    @Autowired
    private JWTUtil jwtUtil;
    //查询社区店铺
    @RequestMapping("/communityGet")
    @ResponseBody
    public CommonResult communityGet(@RequestBody Map<String,String> map){

        Page<Map> page = new Page<>(Integer.parseInt(map.get("current")),Integer.parseInt(map.get("size")));

        Page<Map> community = dlNewStoreService.communityStore(page, map.get("community"));
        CommonResult<Page>  cg = new CommonResult<>(200, "查询成功", community);

        return cg;
    }
    //查询社区店铺
    @RequestMapping("/communityGetName")
    @ResponseBody
    public CommonResult communityGetName(@RequestBody Map<String,String> map){

        Page<Map> page = new Page<>(Integer.parseInt(map.get("current")),Integer.parseInt(map.get("size")));

        Page<Map> community = dlNewStoreService.communityStoreName(page, map);
        log.info("22222" + community.toString());
        CommonResult<Page>  cg = new CommonResult<>(200, "查询成功", community);

        return cg;
    }
    //关闭店铺
    @RequestMapping("/delete")
    @ResponseBody
    public CommonResult delete(@RequestBody Map<String,Integer> map){

        try {
            Integer id = map.get("id");
            DlNewStore byId = dlNewStoreService.getById(id);
            byId.setStatus(-2);

        boolean b = dlNewStoreService.saveOrUpdate(byId);
        if(b){
                return  new CommonResult(200, "店铺已关闭");
            }else {
                return new CommonResult<>(200, "操作失败1");
            }
        }catch (Exception e){

            return new CommonResult<>(200, "操作失败2");
        }
    }
    //修改店铺
    @RequestMapping("/alter")
    @ResponseBody
    public CommonResult alter(@RequestBody Store store){
        DlNewStore dlNewStore = new DlNewStore();
        dlNewStore.setId(store.getId());
        dlNewStore.setName(store.getName());
        dlNewStore.setUser(store.getContacts());
        dlNewStore.setMobile(store.getMobile());
        dlNewStore.setAddress(store.getSite());
        dlNewStore.setLevel(store.getLevel());
        dlNewStore.setFollow(store.getFollow());
        dlNewStore.setStartTime(store.getStartTime());
        dlNewStore.setEndTime(store.getEndTime());
        dlNewStore.setStatus(store.getStatus());
        dlNewStore.setTransportFee(store.getTransportFee());
        dlNewStore.setImg(store.getImg());
        dlNewStore.setImglist(store.getImglist());
        dlNewStore.setSAddress(store.getSAddress());

        boolean b = dlNewStoreService.saveOrUpdate(dlNewStore);
        if(b){
            return new CommonResult(200,"修改成功");
        }else {
            return new CommonResult(200,"修改失败");
        }


    }
    //添加店铺
    @RequestMapping("/addStore")
    @ResponseBody
    public CommonResult addStore(@RequestBody AddStore addStore){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("mobile",addStore.getMobile());
        Integer id = dlUserService.getOne(queryWrapper).getId();

        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("name",addStore.getCommunity());
        Integer id1 = dlNewCommunityService.getOne(queryWrapper1).getId();


        DlNewStore dlNewStore = new DlNewStore();
        dlNewStore.setUid(id);
        dlNewStore.setCid(id1);
        dlNewStore.setName(addStore.getName());
        dlNewStore.setContent(addStore.getContent());
        dlNewStore.setUser(addStore.getUser());
        dlNewStore.setMobile(addStore.getUmobile());
        dlNewStore.setAddress(addStore.getAddress());
        dlNewStore.setStartTime(addStore.getStartTime());
        dlNewStore.setEndTime(addStore.getEndTime());
        dlNewStore.setTransportFee(addStore.getTransportFee());
        dlNewStore.setSAddress(addStore.getSAddress());
        dlNewStore.setImg(addStore.getImg());
        dlNewStore.setImglist(addStore.getImglist());
        dlNewStore.setStatus(1);
        boolean b = dlNewStoreService.saveOrUpdate(dlNewStore);
        if(b){
            return new CommonResult(200,"成功");
        }else {
            return new CommonResult(200,"失败");
        }



    }

    //店铺登入
    @RequestMapping("/storeLogin")
    @ResponseBody
    public CommonResult storeLogin(@RequestBody DlUser dlUser){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("mobile", dlUser.getMobile());
        DlUser user = dlUserService.getOne(wrapper);
        String password = user.getPassword();
        String md51 ="&.@.%.*." + DigestUtils.md5DigestAsHex(dlUser.getPassword().getBytes());
        String md52 = DigestUtils.md5DigestAsHex(md51.getBytes());
        if(password.equals(md52)){
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("uid",user.getId());
            DlNewStore store = dlNewStoreService.getOne(wrapper1);
            HashMap<String, String> map = new HashMap<>();
            String token = jwtUtil.getToken(store.getId(), store.getName());
            map.put("storeId", String.valueOf(store.getId()));
            map.put("storeName",store.getName());
            map.put("token",token);
            return new CommonResult(200,"店铺登入成功",map);
        }
        return new CommonResult(200,"账号或密码出错");
    }

    @RequestMapping("/getId")
    @ResponseBody
    public CommonResult getId(@RequestBody Map<String,Integer> map){
        Integer id = map.get("id");
        DlNewStore store = dlNewStoreService.getById(map.get("id"));
        return new CommonResult(200,"查询成功",store);
    }

    //商家店铺修改
    @RequestMapping("/alters")
    @ResponseBody
    public CommonResult alter(@RequestBody DlNewStore store){
        dlNewStoreService.updateById(store);
        return new CommonResult(200,"修改成功");
    }



}

